//
//  ZDBMyViewController.m
//  ZDB
//
//  Created by Kk on 2020/3/14.
//  Copyright © 2020 ZDB. All rights reserved.
//

#import "ZDBMyViewController.h"
#import "ZDBPersonalCenterHeadView.h"
#import "ZDBPersonalTableViewCell.h"
#import "ZDBMyMessageViewController.h"
#import "ZDBIncomeDetailsViewController.h"
#import "ZDBIntegralDetailsViewController.h"
#import "ZDBWithdrawalsRecordViewController.h"
#import "ZDBBindingAlipayViewController.h"
#import "ZDBExchangeOrderViewController.h"
#import "ZDBEarningCommissionViewController.h"
#import "ZDBHelpViewController.h"
#import "ZDBInviteFriendsViewController.h"
#import "ZDBAddreassViewController.h"
#import "ZDBSetViewController.h"
#import "PresonaModle.h"
#import "ZDBBindWXViewController.h"
#import "ZDBUserInfoAboutViewController.h"
#import "LLWebViewController.h"
@interface ZDBMyViewController ()<UITableViewDelegate,UITableViewDataSource,UINavigationControllerDelegate>
@property (nonatomic,strong)NSMutableArray *dataSource;
@property (nonatomic, strong) UITableView * tableView;
@property (nonatomic, assign) NSInteger msgCount;
@property (nonatomic,strong)ZDBPersonalCenterHeadView * tableviewHeadView;

@end

@implementation ZDBMyViewController
- (void)viewWillAppear:(BOOL)animated
{
     [self statusBarStyleIsDark:NO];
    self.navigationController.delegate = self;
    [self getUserInfo];
    
}
- (void)viewWillDisappear:(BOOL)animated
{
     [self statusBarStyleIsDark:YES];
}
#pragma mark - UINavigationControllerDelegate
// 将要显示控制器
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
 // 判断要显示的控制器是否是自己
  BOOL isShowHomePage = [viewController isKindOfClass:[self class]];
 
 [self.navigationController setNavigationBarHidden:isShowHomePage animated:YES];
}

- (void)viewDidAppear:(BOOL)animated
{
    
    [self getMsgCount];
}
- (void)getMsgCount
{
    WeakSelf
    NSString  * getMsgCount = [[YINetWorkAPIGenerate sharedInstance] APINomark:@"getMsgCount"];
    [[SmileHttpTool sharedInstance] POST:getMsgCount parameters:@{} origin:NO success:^(NSInteger statusCode, NSString *message, id responseObject) {
        
        if (statusCode==0) {
            
            weakSelf.msgCount = [responseObject integerValue];
              [weakSelf.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForItem:0 inSection:0]] withRowAnimation:(UITableViewRowAnimationNone)];
           
        }
        
    } failure:^(id responseObject, NSError *error) {
        
    }];
}
-(void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
//    self.tableviewHeadView.frame = CGRectMake(0, 0, UIScreenWidth, 280 + IPhoneTop);
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.tableView];
    [self showLoadingAnimation];
    _tableviewHeadView = [[NSBundle mainBundle] loadNibNamed:@"ZDBPersonalCenterHeadView" owner:self options:nil].lastObject;
    _tableviewHeadView.frame = CGRectMake(0, 0, UIScreenWidth, 280 + IPhoneTop);
    WEAKSELF
    [_tableviewHeadView.serverBtn add_BtnClickHandler:^(NSInteger tag) {
      //  [[DJLoginHelper sharedInstance] loginOut];
        STRONGSELFFor(weakSelf);
        if (![DJLoginHelper sharedInstance].is_Login) {
                     [[DJLoginHelper sharedInstance]dj_showLoginVC:self];
                    
                     return;
                 }
              
              ZDContactUsBViewController * chatVC = [ZDContactUsBViewController new];
              chatVC.title = @"联系我们";
              [strongSelf.navigationController pushViewController:chatVC animated:YES];
        
    }];
    
    [_tableviewHeadView.infoBtn add_BtnClickHandler:^(NSInteger tag) {
        if (![DJLoginHelper sharedInstance].is_Login)
        {
            [[DJLoginHelper sharedInstance] dj_showLoginVC:self];
            return ;
        }
        STRONGSELFFor(weakSelf);
        ZDBUserInfoAboutViewController * userVC = [ZDBUserInfoAboutViewController new];
        userVC.title = @"个人信息";
        userVC.updetaInfoClickBlock = ^(BOOL isUpdate) {
            [strongSelf getUserInfo];
        };
        [strongSelf.navigationController pushViewController:userVC animated:YES];
    }];
    
  
    _tableView.tableHeaderView =_tableviewHeadView;
    
}
- (void)getUserInfo
{
    
    if (![DJLoginHelper sharedInstance].is_Login) {
         [self stopLoadingAnimation];//
         [self.tableviewHeadView getUserHeadDataWithLoginType:0];
        [self.tableView beginUpdates];
        self.tableviewHeadView.height = 200 + IPhoneTop;
        [self.tableView setTableHeaderView:self.tableviewHeadView];
        [self.tableView endUpdates];
       
        return;
    }
    
    WeakSelf
    NSString  * getUserInfo = [[YINetWorkAPIGenerate sharedInstance] APINomark:@"getUserInfo"];
    [[SmileHttpTool sharedInstance] POST:getUserInfo parameters:@{} origin:YES success:^(NSInteger statusCode, NSString *message, id responseObject) {
        
        NSDictionary * dic = responseObject[@"data"];
        if (!kDictIsEmpty(dic)) {
            [[DJLoginHelper sharedInstance]save_UserInfo:dic];
            if (![DJLoginHelper sharedInstance].is_BindWX && [DJLoginHelper sharedInstance].is_Login) {
                 [self getBindWXStatus];
            }else
            {
                 [weakSelf.tableviewHeadView getUserHeadDataWithLoginType:1];
            }
            if (self.tableviewHeadView.height ==200 + IPhoneTop) {
               
                [self.tableView beginUpdates];
                self.tableviewHeadView.height = 280 + IPhoneTop;
                [self.tableView setTableHeaderView:self.tableviewHeadView];
                [self.tableView endUpdates];
            }
            [self.tableView reloadData];
        }else
        {
            if (self.tableviewHeadView.height ==280 + IPhoneTop) {
                [self.tableviewHeadView getUserHeadDataWithLoginType:0];
                [self.tableView beginUpdates];
                self.tableviewHeadView.height = 200 + IPhoneTop;
                [self.tableView setTableHeaderView:self.tableviewHeadView];
                [self.tableView endUpdates];
            }
            [self.tableView reloadData];
           
            [JMNotifyView showNotify:responseObject[@"message"] isSuccessful:NO];
        }
        [weakSelf stopLoadingAnimation];//
    } failure:^(id responseObject, NSError *error) {
        [weakSelf stopLoadingAnimation];
    }];
}
- (void)getBindWXStatus
{
    WeakSelf
       NSString  * getUserInfo = [[YINetWorkAPIGenerate sharedInstance] APINomark:@"selectWxNumberIsBand"];
       [[SmileHttpTool sharedInstance] POST:getUserInfo parameters:@{} origin:NO success:^(NSInteger statusCode, NSString *message, id responseObject) {
           
           if (kObjectIsEmpty(responseObject)) {
                [[DJLoginHelper sharedInstance]saveWXExamineType:@"0"];
                [weakSelf.tableviewHeadView getUserHeadDataWithLoginType:2];
           }else
           {
               NSDictionary * dataDicc = [NSDictionary dictionaryWithDictionary:responseObject];
               NSString *  state = [dataDicc jsonString:@"state"];
               [[DJLoginHelper sharedInstance]saveWXExamineType:state];
               [weakSelf.tableviewHeadView getUserHeadDataWithLoginType:2];
           }
           
       } failure:^(id responseObject, NSError *error) {
          
       }];
}
- (void)axcBaseRequestData
{
    NSMutableArray * dataAry = [NSMutableArray new];
    [dataAry  addObject:@{@"image":@"news",@"title":@"我的消息",@"controllerClass":[ZDBMyMessageViewController class]}];
    [dataAry addObject:@{@"image":@"shouru",@"title":@"收入明细",@"controllerClass":[ZDBIncomeDetailsViewController class]}];
    [dataAry addObject:@{@"image":@"jifen",@"title":@"积分明细",@"controllerClass":[ZDBIntegralDetailsViewController class]}];
    [dataAry addObject:@{@"image":@"tixian",@"title":@"提现记录",@"controllerClass":[ZDBWithdrawalsRecordViewController class]}];
    [dataAry addObject:@{@"image":@"alipay",@"title":@"绑定支付宝",@"controllerClass":[ZDBBindingAlipayViewController class]}];
    [dataAry addObject:@{@"image":@"change",@"title":@"兑换订单",@"controllerClass":[ZDBExchangeOrderViewController class]}];
    [dataAry addObject:@{@"image":@"zhuany",@"title":@"如何赚佣",@"controllerClass":[ZDBEarningCommissionViewController class]}];
    [dataAry addObject:@{@"image":@"help",@"title":@"帮助中心",@"controllerClass":[ZDBHelpViewController class]}];
    [dataAry addObject:@{@"image":@"invite",@"title":@"邀请好友",@"controllerClass":[ZDBInviteFriendsViewController class]}];
    [dataAry addObject:@{@"image":@"address",@"title":@"我的地址",@"controllerClass":[ZDBAddreassViewController class]}];
    [dataAry addObject:@{@"image":@"setting",@"title":@"设置",@"controllerClass":[ZDBSetViewController class]}];
    
    
    for (NSDictionary *dic in dataAry)
    {
        PresonaModle *modle=[[PresonaModle alloc]init];
        modle.title=dic[@"title"];
        modle.image=dic[@"image"];
        modle.controllerClass=dic[@"controllerClass"];
        [self.dataSource addObject:modle];
    }
    [self.tableView reloadData];
}
- (NSMutableArray *)dataSource
{
    if (!_dataSource) {
        _dataSource = [NSMutableArray new];
    }
    return _dataSource;
}
#pragma  mark -----懒加载
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, UIScreenWidth, UIScreenHeight  - TABBAR_HEIGHT)style:UITableViewStyleGrouped];
        _tableView.tableFooterView = [UIView new];
        _tableView.separatorColor = CELLCOLOR;
        _tableView.delegate = self;
        _tableView.dataSource=self;
        _tableView.backgroundColor =  VIEWBGCOLOR;
        _tableView.showsVerticalScrollIndicator = NO;
       _tableView.alwaysBounceVertical = NO;
        _tableView.bounces = NO;
        [_tableView registerNib:[UINib nibWithNibName:@"ZDBPersonalTableViewCell" bundle:nil]  forCellReuseIdentifier:@"ZDBPersonalTableViewCell"];
    
    }
    return _tableView;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ZDBPersonalTableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:@"ZDBPersonalTableViewCell" forIndexPath:indexPath];
    cell.model = _dataSource[indexPath.row];
    cell.msgCount.hidden = YES;
    if (indexPath.row==0) {
        if ( self.msgCount>0) {
            cell.msgCount.hidden = NO;
            cell.msgCount.text = kFormat(@"%ld", (long)self.msgCount);
        }
    }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (![ZBDUnit getLoginJurisdiction]) {
        return;
    }
     PresonaModle *si = _dataSource[indexPath.row];
    if ([si.controllerClass  isEqual:[ZDBHelpViewController class]]) {
        LLWebViewController *webV = [LLWebViewController new];
        NSString * url = [[YINetWorkAPIGenerate sharedInstance] APINomark:@"help"];
        webV.urlStr = url;
        webV.isPullRefresh = YES;
        [self.navigationController pushViewController:webV animated:YES];
    }else
    {
        ZBDBaseViewController *vc = [[si.controllerClass alloc] init];
        vc.navigationItem.title = si.title;
        [self.navigationController pushViewController:vc animated:YES];
    }
   
    
    
}
@end
